package com.apes.hr.paySupport.service;

import com.alibaba.fastjson.JSON;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.workflow.WorkFlowService;
import com.apes.framework.plugin.workflow.lib.WorkflowApi;
import com.apes.framework.query.SqlService;
import com.apes.framework.util.MapUtil;
import com.apes.hr.paySupport.model.SupportApplicationAffirm;
import com.apes.hr.paySupport.repository.SupportApplicationAffirmRepository;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Title: AutoApproveWorkFlowService
 * @Description: 自动审核OA流程
 * @author: ZhengZeXian
 * @create: 2021/4/25 16:34
 */
@Service("autoApproveWorkFlowService")
public class AutoApproveWorkFlowService extends DomainService {

    @Autowired
    private WorkflowApi workflowApi;
    @Autowired
    private SqlService sqlService;
    @Autowired
    private SupportApplicationAffirmRepository supportApplicationAffirmRepository;
    @Autowired
    private WorkFlowService workFlowService;

    private final long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
    private final long nh = 1000 * 60 * 60;// 一小时的毫秒数
    private final long nm = 1000 * 60;// 一分钟的毫秒数

    /**
     * 根据某个OA流程 找到执行的任务
     * @param procdefId
     * @return
     */
    public JSONArray getOA(String procdefId){
        String sSQL = "SELECT B.BUSINESS_KEY_,A.* FROM  ACT_RU_TASK A,ACT_HI_PROCINST B WHERE A.PROC_INST_ID_ = B.PROC_INST_ID_ AND A.PROC_DEF_ID_ LIKE '%" + procdefId + "%'";
        return sqlService.query(sSQL);
    }

    /**
     * 定时任务 审核支援确认单
     * @param request
     * @throws Exception
     */
    public void timingApproveAffirm(SimpleRequest request) throws Exception {
        //初始化
        JSONArray array = new JSONArray();

        //数据获取
        JSONArray zy = getOA("hr.supportApplication_affirm_zy.OA");
        JSONArray fn = getOA("hr.supportApplication_affirm_fn.OA");
        JSONArray dz = getOA("hr.supportApplication_affirm_dz.OA");

        array.addAll(zy);
        array.addAll(fn);
        array.addAll(dz);

        Date date = new Date();

        //待处理数据存放
        List<Map> list = new ArrayList<>();

        for (int i = 0; i < array.size(); i++) {
            Date createTime = (Date) array.get(i).get("CREATE_TIME_");

            //计算具体的小时
            Long diff = (date.getTime() - createTime.getTime())/1000;
            Long hour = (diff/60)/60;
//            Long day = diff / nd;
//            Long hour = diff % nd / nh;

           //当前节点为支援人直接跳过
            if("支援人".equals(array.get(i).getString("NAME_"))){
                continue;
            }
            if (hour >= 72){
                list.add(MapUtil.mapper(
                        "taskId",array.get(i).get("ID_"),
                        "businessKey",array.get(i).get("BUSINESS_KEY_"),
                        "assignee",array.get(i).get("ASSIGNEE_")
                ));
            }
        }
        //逻辑执行
        if (!list.isEmpty()){
            list.stream().forEach(item ->{
                SupportApplicationAffirm affirm = supportApplicationAffirmRepository.findOne(item.get("businessKey").toString());
                if(affirm!=null){
                    Map map = MapUtil.mapped(affirm);
                    //获取经办人没有的情况下使用超级管理员
                    workFlowService.assignTaskExecute(map,item.get("taskId").toString(),1,item.get("assignee") == null ? "1" : item.get("assignee").toString());
                    workflowApi.executeTask(item.get("taskId").toString(),map);
                }
            });
        }
    }


}
